﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Test
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            RunTest();
        }

        private void RunTest()
        {
            string path = GJQT.Repository.GetInstallationPath();
            string filename = Path.Combine(path, @"Data\UIData.zpk");
            //string filename = Path.Combine(path, @"Data\Music.zpk");
            GJQT.ZPK.ZpkArchive ar = new GJQT.ZPK.ZpkArchive(filename);
            foreach (GJQT.ZPK.ZpkArchiveEntry entry in ar.Entries)
            {
                listBox1.Items.Add(entry);
                if (entry.FullName.EndsWith(".png"))
                    listBox1.SelectedIndex = listBox1.Items.Count - 1;
            }
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            GJQT.ZPK.ZpkArchiveEntry entry =
                listBox1.SelectedItem as GJQT.ZPK.ZpkArchiveEntry;
            if (entry == null)
                return;

            // Create a temporary file to store the contents.
            string filename = Path.Combine(
                Path.GetFullPath(@"..\.."),
                Path.GetFileName(entry.FullName));

            using (Stream output = File.Create(filename))
            using (Stream input = entry.Open())
            {
                byte[] buffer = new byte[10000];
                while (true)
                {
                    int actual = input.Read(buffer, 0, buffer.Length);
                    if (actual == 0)
                        break;
                    output.Write(buffer, 0, actual);
                }
            }

            // Open the output file using the default editor.
            //System.Diagnostics.Process.Start(filename);
        }
    }
}
